package com.appview;


import com.dao.StudentDao;
import com.dao.TeacherDao;
import com.model.Student;
import com.util.Config;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import javax.swing.table.DefaultTableModel;

/*
管理员主界面
 */
public class MainView extends JFrame implements ActionListener, MenuListener {
	private static MainView mainView;

	private JMenuBar menubar;
	private JMenu menu1, menu2,menu3,menu4,menu5;
	private JMenuItem menuItem;
	public JTable jTable1;
	private JScrollPane jScrollPane;
	private JPanel jPanel, jPanel1, jP_select, jp_btn;
	private JButton jButton, jButto1, jButton2, jButton3, btnList, btnAdd, btnCommit;
	private JLabel jL_select;
	public JComboBox jcb;
	public DefaultTableModel tableModel;
	//界面 类型


	public MainView(){
		mainView = MainView.this;
	}

	public static MainView getMainView() {
		return mainView;
	}

	public void openView() {
		jPanel1 = new JPanel();
		jScrollPane = new JScrollPane();
		if (Config.getType() == 0) {
			setTitle("教学信息管理系统（当前为教师管理）");
		} else {
			setTitle("教学信息管理系统（当前为学生管理）");
		}

		setSize(1000, 600);
		setLocation(200, 80);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
		// 创建菜单条和菜单
		menubar = new JMenuBar();
		menubar.setBorderPainted(true);

		if (Config.getType() == 0) {
			menu1 = new JMenu("教师信息");
			menu3 = new JMenu("教师班级信息");
		} else {
			menu1 = new JMenu("学生信息");
			menu3 = new JMenu("学生个人成绩");
		}

		menuItem = new JMenuItem("系统功能");
		menu2 = new JMenu("安排课表");
		menu4 = new JMenu("修改密码");
		menu5 = new JMenu("其他");
		menu5.add(menuItem);
		menubar.add(menu1);
		menubar.add(menu2);
//		menubar.add(menu3);
//		menubar.add(menu4);
		menubar.add(menu5);

		add(menubar, BorderLayout.NORTH);
		menu1.addMenuListener(this);
		menu2.addMenuListener(this);
		menu3.addMenuListener(this);
		menu4.addMenuListener(this);
		menuItem.addActionListener(this);

	}


	//显示教师或学生信息
	public void informationTable() {
		//重画界面

		this.remove(jScrollPane);
		this.remove(jPanel1);
		this.invalidate();
//		jButton = new JButton("查询");
		jButto1 = new JButton("增加");
		jButton2 = new JButton("修改");
		jButton3 = new JButton("删除");
//		jButton.addActionListener(this);
		jButto1.addActionListener(this);
		jButton2.addActionListener(this);
		jButton3.addActionListener(this);
		jPanel1 = new JPanel();
//		jPanel1.add(jButton);
		jPanel1.add(jButto1);
		jPanel1.add(jButton2);
		jPanel1.add(jButton3);

		String list[] = null;
		if (Config.getType() == 0) {
			list = Config.TEAC_LIST;
		} else {
			list = Config.STU_LIST;
		}
		tableModel = new DefaultTableModel(list, 0) {
			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		jTable1 = new JTable(tableModel);
		jScrollPane = new JScrollPane(jTable1);

		this.add(jScrollPane, BorderLayout.CENTER);
		this.add(jPanel1, BorderLayout.SOUTH);

		repaintView();

	}

	//显示课表信息
	public void courseTable() {
		jPanel1.removeAll();
		this.remove(jScrollPane);
		this.remove(jPanel1);

		this.invalidate();

		jTable1 = TableModel.getCourseTable();
		jTable1.setSelectionBackground(Color.CYAN);
		jScrollPane = new JScrollPane(jTable1);
		if (Config.getType() == 0) {
			jL_select = new JLabel("选择教师安排课表：");
			jcb = new JComboBox(TeacherDao.getInstance().getTList());
		} else {
			jL_select = new JLabel("选择专业安排课表：");
			jcb = new JComboBox(StudentDao.getInstance().getMajorList());
		}


		btnList = new JButton("查看课表");
		jP_select = new JPanel();
		jP_select.add(jL_select);
		jP_select.add(jcb);
		jP_select.add(btnList);
		jP_select.setLayout(new FlowLayout(FlowLayout.CENTER));
		btnAdd = new JButton("添加");
		btnCommit = new JButton("提交");
		btnAdd.addActionListener(this);
		btnCommit.addActionListener(this);
		btnList.addActionListener(this);
		jp_btn = new JPanel();
		jPanel1.add(jP_select);
		jp_btn.add(btnAdd);
		jp_btn.add(btnCommit);
		jp_btn.setLayout(new FlowLayout(FlowLayout.LEFT));
		jPanel1.add(jp_btn);
		jPanel1.setLayout(new GridLayout(1, 2, 0, 3));

		this.add(jPanel1, BorderLayout.SOUTH);
		this.add(jScrollPane, BorderLayout.CENTER);

		repaintView();
	}

	//重画界面
	public void repaintView() {
		this.repaint();
		this.validate();
		this.invalidate();
		this.validate();
	}


	//按键监听
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == jButto1) {
			//添加
			InfoOperation infoOperation = new InfoOperation(0);
			infoOperation.openView();
		}else if (e.getSource() == jButton2) {
			//修改
			try {
				InfoOperation infoOperation = new InfoOperation(1, TableInfoTools.getModifyInfo(jTable1));
				infoOperation.openView();
			} catch (ArrayIndexOutOfBoundsException e1) {
				JOptionPane.showMessageDialog(this, "请选中其中一行", "Error", JOptionPane.WARNING_MESSAGE);
			} finally {
				informationTable();
				TableInfoTools.printInfo(tableModel);
			}

		}else if (e.getSource() == jButton3) {
			//删除
			try {
				if (jTable1.getSelectedRow() == -1) {
					throw new ArrayIndexOutOfBoundsException() ;
				}
				int select = JOptionPane.showConfirmDialog(this, "确定删除该条信息？", "删除操作", JOptionPane.YES_NO_OPTION);
				if (select == 0) {
					TableInfoTools.deleteInfoRow(jTable1);
				}
			} catch (ArrayIndexOutOfBoundsException e1) {
				JOptionPane.showMessageDialog(this, "请选中其中一行", "Error", JOptionPane.WARNING_MESSAGE);
			} finally {
				informationTable();
				TableInfoTools.printInfo(tableModel);
			}
		}

		if (e.getSource() == btnAdd) {
			AddCourse add = null;
			try {
				add = new AddCourse(jTable1.getSelectedRow(), jTable1.getSelectedColumn(), (String) jcb.getSelectedItem());
				add.openView();
			} catch (ArrayIndexOutOfBoundsException e1) {
				JOptionPane.showMessageDialog(this, "请选中其中一行", "Error", JOptionPane.WARNING_MESSAGE);
			}
		} else if (e.getSource() == btnCommit){
			//提交
			TableInfoTools.saveCourse(jTable1);
		} else if (e.getSource() == btnList) {

			TableInfoTools.printTeaCourse(jTable1, (String) jcb.getSelectedItem());

		} else if (e.getSource() == menuItem) {
			//显示系统设置
			ExitTip exitTip = new ExitTip();
			exitTip.openView();
		}
	}

	//菜单监听
	@Override
	public void menuSelected(MenuEvent e) {
		if (e.getSource() == menu1) {
			//显示教师或学生信息
			informationTable();
			TableInfoTools.printInfo(tableModel);
		} else if (e.getSource() == menu2) {
			courseTable();
		}

	}

	@Override
	public void menuDeselected(MenuEvent e) {

	}

	@Override
	public void menuCanceled(MenuEvent e) {

	}



}

